Updating a static
field from a non-static
method introduces significant challenges and potential bugs. Multiple class
instances and threads can access and modify the static
field concurrently, leading to unintended consequences for other instances or
threads (unexpected behavior, race conditions and
synchronization problems).
class MyClass
{
private static int count = 0;
public void DoSomething()
{
//...
count++; // Noncompliant: make the enclosing instance property 'static' or remove this set on the 'static' field.
}
}
interface MyInterface
{
private static int count = 0;
public void DoSomething()
{
//...
count++; // Noncompliant: remove this set, which updates a 'static' field from an instance method.
}
}